package com.jinlejia.bbs.logic;

import android.content.Context;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.jinlejia.bbs.http.utils.BBSConfig;
import com.jinlejia.bbs.http.utils.HttpCallBack;

import static com.jinlejia.bbs.http.utils.HttpUtils.JsonParse;
import static com.jinlejia.bbs.http.utils.HttpUtils.httpCallBack;
import static com.jinlejia.bbs.http.utils.HttpUtils.httpCallForm;

/**
 * @author 王进 on 2019/4/2.
 */
//评价
public class CommentLogic {
    private Context context;
    public CommentLogic(Context context){
        this.context = context;
    }

    public static final class CommentLogicSource{
        private String answerCode;
        private String commentsContent;
        private String toPin;
        private String toName;
        private String commentsCode;

        public void setCommentsCode(String commentsCode) {
            this.commentsCode = commentsCode;
        }

        private int type;
        public void setAnswerCode(String answerCode) {
            this.answerCode = answerCode;
        }

        public void setCommentsContent(String commentsContent) {
            this.commentsContent = commentsContent;
        }

        public void setToPin(String toPin) {
            this.toPin = toPin;
        }

        public void setToName(String toName) {
            this.toName = toName;
        }

        void setType(int type) {
            this.type = type;
        }
    }

    public interface CommentLogicCallBack{
        void onResult(boolean result);
    }
    /**
     * 提交文本评价
     */
    public void submitMessage(CommentLogicSource source,CommentLogicCallBack callBack){
        source.setType(1);
        send(source,callBack);
    }

    private void send(CommentLogicSource source,final CommentLogicCallBack callBack){
        JSONObject json = new JSONObject();
        json.put("answerCode",source.answerCode);
        json.put("commentsType",source.type);
        json.put("commentsContent",source.commentsContent);
        if (!TextUtils.isEmpty(source.toPin) && !TextUtils.isEmpty(source.toName)){
            json.put("toPin",source.toPin);
            json.put("toName",source.toName);
            json.put("commentsCode",source.commentsCode);
        }

        httpCallForm(BBSConfig.saveComments,json)
                .execute(httpCallBack(context, new HttpCallBack() {
                    @Override
                    public void onError(Exception e) {
                        callBack.onResult(false);
                    }

                    @Override
                    public void onResponse(JsonParse parse) {
                        boolean result = parse.parse(Boolean.class);
                        callBack.onResult(result);
                    }

                    @Override
                    public void onNoResponse(int code) {
                        callBack.onResult(false);
                    }
                }));
    }
}
